 var DeviceMain;//主头
	var DeviceAssist;//副头
	var Video;//视频
	
	function plugin()
    {
        return document.getElementById('view1');
    }

    function view()
    {
        return document.getElementById('view1');
    }
    	
	function thumb()
    {
       // return document.getElementById('thumb1');
    }
	
	function addEvent(obj, name, func)
    {
        if (obj.attachEvent) {
            obj.attachEvent("on"+name, func);
        } else {
            obj.addEventListener(name, func, false); 
        }
    }
	
	function OpenVideo()
	{	
		
		var sDevice =   document.getElementById('device');
		
		if(sDevice.selectedIndex != -1)
		{
			CloseVideo();
			plugin().Device_GetIndex(DeviceAssist);
		    var dev = DeviceAssist;//选中副头
		    SelectType = 2;			
			
			Video = plugin().Device_CreateVideo(dev, 1, SelectType);
			if (Video)
			{
				view().View_SelectVideo(Video);
				view().View_SetText("视频打开中，请将面部对准摄像头...", 0);
				var initFaceDetectSuccess = plugin().InitFaceDetect();
				if(!initFaceDetectSuccess)
				{
					alert("人脸初始化失败，机型不支持！");
					obj.checked = false;
				} else {
					plugin().Global_EnableFaceRectCrop(Video, 1);
				}		
					
			}				
		}
	}
	
	function CloseVideo()
	{
		if (Video)
		{
			view().View_SetText("", 0);
			plugin().Video_Release(Video);
			Video = null;
		}		
	}
	
	//切换设备
	function changeDev()
	{
		
		var sDevice =   document.getElementById('device');
		sDevice.selectedIndex = 1;
		if(sDevice.selectedIndex != -1)
		{							
			plugin().Device_GetIndex(DeviceAssist)
			var dev = DeviceAssist;						

			plugin().Device_GetSubtype(dev);
		    				
			SelectType = 2;
			    							
			plugin().Device_GetResolutionCountEx(dev, SelectType);//根据出图模式获取分辨率
			
		}
	}		


	function Load()
	{
		//设备接入和丢失
		//type设备类型， 1 表示视频设备， 2 表示音频设备
		//idx设备索引
		//dbt 1 表示设备到达， 2 表示设备丢失
		addEvent(plugin(), 'DevChange', function (type, idx, dbt) 
		{
			if(1 == type)//视频设备
			{
				if(1 == dbt)//设备到达
				{
					var deviceType = plugin().Global_GetEloamType(1, idx);
					if(1 == deviceType)//主摄像头
					{
						if(null == DeviceMain)
						{
							DeviceMain = plugin().Global_CreateDevice(1, idx);										
							if(DeviceMain)
							{
							
								var sDevice =   document.getElementById('device');
								
								sDevice.add(new Option(plugin().Device_GetFriendlyName(DeviceMain)));
								sDevice.selectedIndex = 1;								
								
							}
						}
					}
					else if(2 == deviceType || 3 == deviceType)//辅摄像头
					{
						if(null == DeviceAssist)
						{
							DeviceAssist = plugin().Global_CreateDevice(1, idx);										
							if(DeviceAssist)
							{				
								
								var sDevice =   document.getElementById('device');							
								
								sDevice.add(new Option(plugin().Device_GetFriendlyName(DeviceAssist)));	
								
								changeDev();
								
								//OpenVideo();//是主头自动打开视频
								
							}
						}
					}
				}
				else if(2 == dbt)//设备丢失
				{
					if (DeviceMain) 
					{
						if (plugin().Device_GetIndex(DeviceMain) == idx) 
						{
							CloseVideo();
							plugin().Device_Release(DeviceMain);
							DeviceMain = null;
							
							document.getElementById('device').options.length = 0; 
						 
						}
					}
					
					if (DeviceAssist) 
					{
						if (plugin().Device_GetIndex(DeviceAssist) == idx) 
						{
							CloseVideo();
							plugin().Device_Release(DeviceAssist);
							DeviceAssist = null;
							
							document.getElementById('device').options.length = 0; 
							
						}
					}
				}
			}
		});
		
		addEvent(plugin(), 'MoveDetec', function(video, id){
		    // 自动拍照事件	
			
			plugin().MoveDetec(video, id);
			
			plugin().Video_EnableMoveDetec(video, true);
			
		});
		
		var title = document.title;
		document.title = title + plugin().version;
	
		view().Global_SetWindowName("view");

		plugin().Global_InitDevs();
	}
		
	function Unload()
	{
		if (Video)
		{
			view().View_SetText("", 0);
			plugin().Video_Release(Video);
			Video = null;
		}
		if(DeviceMain)
		{
			plugin().Device_Release(DeviceMain);
			DeviceMain = null;	
		}
		if(DeviceAssist)
		{
			plugin().Device_Release(DeviceAssist);
			DeviceAssist = null;	
		}
		plugin().Global_DeinitDevs();
	}
	
	function Scan()
	{	
		var date = new Date();
		var yy = date.getFullYear().toString();
		var mm = (date.getMonth() + 1).toString();
		var dd = date.getDate().toString();
		var hh = date.getHours().toString();
		var nn = date.getMinutes().toString();
		var ss = date.getSeconds().toString();
		var mi = date.getMilliseconds().toString();
		var Name = "D:\\" + yy + mm + dd + hh + nn + ss + mi + ".jpg";
	
		var img = plugin().Video_CreateImage(Video, 0, view().View_GetObject());
		var bSave = plugin().Image_Save(img, Name, 0);
		if (bSave)
		{
			view().View_PlayCaptureEffect();
		}
		
		plugin().Image_Release(img);
	}
	
   
	//拍照
    function headScan()
    {
        var img_base64;
        var img = plugin().Video_CreateImage(Video, 0, view().View_GetObject());
        if (Video) {

            img_base64 = plugin().Image_GetBase64(img, 2, 0x0100);
            console.log('scan result base 64: ', img_base64);
            if (img_base64) {
            	view().View_PlayCaptureEffect();
            }

            plugin().Image_Release(img);
           
        }
        
        var base64Img = 'data:image/jpeg;base64,' + img_base64;
        
        $('#faceImg').attr('src', base64Img);
 
    }
       
    function VerifyFaceDetect()
 {
     var flag = -1;
     if (Video)
     {


         var imgBase64 = $("#certImg").val();
         if(undefined == imgBase64 || imgBase64 == ''){
             console.log('e data:', "imgBase64 is null");
             alert("没有获取到该账号的认证头像，请使用其它方式登录！");
             return -2;
         }
         var certHeadImage = plugin().Global_CreateImageFromBase64(imgBase64, 0);
         var image = plugin().Video_CreateImage(Video,0, view().View_GetObject());

         if(image && certHeadImage)
         {
             var ret = plugin().DiscernFaceDetect(image, certHeadImage);
             if(ret != -1)
             {
                 ret += 20;
                 if(ret > 100)
                 {
                     ret = 100;
                 }
                 var msg = "识别已完成，匹配度：" + ret + "\r\n（匹配阈值为70，高于阈值则为同一人）";
                 console.log('msg data:', msg);
                 view().View_SetText("人脸识匹配度为:" + ret + "，正在登录...", 0);
                 return ret;
             }
             else
             {
                 alert("头像认证失败，请使用其它方式登录！");
                 
                 view().View_SetText("头像认证失败，请使用其它方式登录！", 0);
                 return -2;
             }
         }
         else
         {
             alert("获取图像失败，该用户没有上传认证头像！");
             view().View_SetText("获取图像失败，该用户没有上传认证头像！", 0);
             return -2;
         }

         plugin().Image_Release(image);
         plugin().Image_Release(certHeadImage);
     }
     return flag;
 }
    
    
    